#!/bin/bash
# build.sh 
# 可以用来构建前端和后端的模块

set -e

##################### 获取工作目录 #####################
# 参考：http://www.gimoo.net/t/1502/54ea928e14fef.html
SOURCE="$0"
while [ -h "$SOURCE"  ]; do                              # resolve $SOURCE until the file is no longer a symlink
    DIR="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"
    SOURCE="$(readlink "$SOURCE")"
    [[ $SOURCE != /*  ]] && SOURCE="$DIR/$SOURCE"        # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE"  )" && cd .. && pwd  )"
#######################################################

app=$(basename "$DIR")   # app="ems"
workdir=$DIR             # workdir="/opt/$app"

# 参数：<模块>，可能的值：
#   固定为 archive
module='archive'
branch='develop'
src_module="ems-archive"

# 使用说明
usage() {
    echo -e "HELP: 用法: ./pull.sh"
    exit 1
}

echo "INFO: [$module] 主机：$(hostname) 分支：$branch"

# 拉最新代码
git_pull(){
  echo "INFO: [$module] 拉取 $branch 分支的最新代码"
  cd "$workdir/src/$src_module"
  echo "INFO: [$module] 切换到分支：[$branch]"
  git checkout "$branch"
  echo "INFO: [$module] 拉取代码"
  git pull origin "$branch"
}

git_pull
